<template>
	<!-- 视频预览 -->
	<uv-popup :overlayStyle="{ background: 'rgba(0, 0, 0, 1)' }" ref="videoPopup" :safeAreaInsetBottom="false"
		bgColor="#000000" @maskClick="maskClick">
		<!-- autoplay @click="maskClick"-->
		<video v-if="currVideo" ref="myVideo" class="video-pre" :src="currVideo" autoplay loop :controls="false"
			@click="maskClick"></video>
	</uv-popup>
</template>

<script>
	export default {
		props: ['fileObj'],
		data() {
			return {
				currVideo: '',
			}
		},
		watch: {
			fileObj(newValue) {
				if (newValue && typeof newValue == 'object') {
					this.$emit('onFileSuccess', '');
					let file = newValue;
					console.log(file);
					let type = this.isFileUrl(file.file_suffix)

					switch (type) {
						case 'img':
							uni.previewImage({
								urls: [file.file_url]
							});
							break;
						case 'video':
							let that = this
							this.currVideo = file.file_url;
							this.$refs.videoPopup.open()
							break;
						case 'wps':
							uni.showLoading({
								mask: true
							});
							uni.downloadFile({
								url: file.file_url,
								success: function(res) {
									var filePath = res.tempFilePath;
									uni.openDocument({
										filePath: filePath,
										showMenu: true,
										fileType: file.file_suffix,
										success: function(res) {
											uni.hideLoading();
											console.log('打开文档成功');
										},
										fail(err) {
											console.log(err);
											uni.hideLoading();
										}
									});
								},
								fail(err) {
									console.log(err);
									uni.hideLoading();
								}
							});
							break;
						case 'other':
							uni.setClipboardData({
								data: file.file_url, //要被复制的内容
								success: () => {
									uni.showModal({
										title: '提示',
										content: '链接复制成功，可前往浏览器下载查看',
										showCancel: false
									});
								}
							});
							break;
					}
				}
			},
		},
		methods: {
			isFileUrl(fileSuffix) {
				let app = getApp()
				let imgExtensions = app.globalData.imgExtensions
				let videoExtensions = app.globalData.videoExtensions
				let fileExtensions = app.globalData.fileExtensions

				if (imgExtensions.includes(fileSuffix)) {
					return 'img'
				} else if (videoExtensions.includes(fileSuffix)) {
					return 'video'
				} else if (fileExtensions.includes(fileSuffix)) {
					return 'wps'
				} else {
					return 'other'
				}
			},
			videoPause() {
				this.opause = true;
			},
			maskClick(type) {
				this.currVideo = ""
				this.$refs.videoPopup.close()
				// if (type == 'overlay') {
				// 	this.currVideo = ""
				// 	this.$refs.videoPopup.close()
				// }
				// if (this.opause) {
				// 	this.currVideo = ""
				// 	this.opause = false
				// 	this.$refs.videoPopup.close()
				// }
			},
		}
	}
</script>

<style lang="scss" scoped>
	.video-pre {
		width: 750rpx;
		height: 60vh;
	}
</style>